package com.aigou.admin.dao.impl;

import com.aigou.admin.dao.ShopCartDAO;
import com.aigou.admin.entity.ShopCart;
import com.aigou.admin.util.JDBCUtils;

import java.util.List;

public class ShopCartDAOImpl implements ShopCartDAO {
    @Override
    public List<ShopCart> getAllShopCarts() {
        return JDBCUtils.executeQuery(ShopCart.class, "SELECT * FROM s_shop_cart");
    }

    @Override
    public ShopCart getShopCartById(String id) {
        return (ShopCart) JDBCUtils.executeQuery(ShopCart.class, "SELECT * FROM s_shop_cart WHERE id = ?", id).get(0);
    }

    @Override
    public int addShopCart(ShopCart shopCart) {
        return JDBCUtils.executeUpdate("INSERT INTO s_shop_cart(id, cart_id, user_id) VALUES(?, ?, ?)", shopCart.getId(), shopCart.getCart_id(), shopCart.getUser_id());
    }

    @Override
    public int deleteShopCart(String id) {
        return JDBCUtils.executeUpdate("DELETE FROM s_shop_cart WHERE id = ?", id);
    }

    @Override
    public int updateShopCart(ShopCart shopCart) {
        return JDBCUtils.executeUpdate("UPDATE s_shop_cart SET cart_id = ?, user_id = ? WHERE id = ?", shopCart.getCart_id(), shopCart.getUser_id(), shopCart.getId());
    }

    @Override
    public List<ShopCart> findShopCartByUserId(String userId) {
        return JDBCUtils.executeQuery(ShopCart.class, "SELECT * FROM s_shop_cart WHERE user_id = ?", userId);
    }

    @Override
    public Integer selectShopCartCount() {
        return JDBCUtils.executeCount("SELECT count(*) FROM s_shop_cart");
    }

    @Override
    public List<ShopCart> selectShopCartLimits(int start, int row) {
        return JDBCUtils.executeQuery(ShopCart.class, "SELECT * FROM s_shop_cart LIMIT ?, ?", start, row);
    }
}
